package cn.stronglink.assetmanage.utils.dao;

import java.util.ArrayList;
import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.ModelDao;
import cn.stronglink.assetmanage.entity.Brand;
import cn.stronglink.assetmanage.entity.Model;
import cn.stronglink.assetmanage.entity.Room;
import cn.stronglink.assetmanage.entity.Type;

/**
 * 基础数据工具类
 * Author by chencongcong,Date on 2018/9/20
 */
public class BaseDataUtil {
    /**
     *获取类型列表
     * @return
     */
    public static List<Type> getTypeList() {
        List<Type> typeList = new ArrayList<>();
        try {
            Type type = new Type();
            type.setId_("");
            type.setName("请选择类型");
            typeList.add(type);

            List<Type> types = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getTypeDao().loadAll();
            if (types != null && types.size() > 0) {
                typeList.addAll(types);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return typeList;
    }

    /**
     * 获取品牌列表
     * @return
     */
    public static List<Brand> getBrandList() {
        List<Brand> brandList = new ArrayList<>();
        try {
            Brand brand = new Brand();
            brand.setId_("");
            brand.setName("请选择品牌");
            brandList.add(brand);
            List<Brand> brands = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getBrandDao().loadAll();
            if (brands != null && brands.size() > 0) {
                brandList.addAll(brands);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return brandList;
    }

    /**
     * 获取型号列表
     * @return
     */
    public static List<Model> getModelList() {
        List<Model> modelList = new ArrayList<>();
        Model model = new Model();
        model.setBrandId_("");
        model.setTypeId_("");
        model.setId_("");
        model.setName("请选择型号");
        modelList.add(model);
        List<Model> models = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getModelDao().loadAll();
        if (models!=null&&models.size()>0){
            modelList.addAll(models);
        }
        return modelList;
    }

    /**
     * 获取地点列表
     * @return
     */
    public static List<Room> getRoomList() {
        List<Room> roomList = new ArrayList<>();
        try {
            Room room = new Room();
            room.setId_("");
            room.setName("请选择地点");
            roomList.add(room);
            List<Room> rooms = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRoomDao().loadAll();
            if (rooms != null && rooms.size() > 0) {
                roomList.addAll(rooms);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return roomList;
    }

    /**
     * 获取型号列表
     * @param brandId
     * @param typeId
     * @return
     */
    public static  List<Model> queryModelByBrandAndType(String brandId,String typeId) {
        List<Model> modelList = new ArrayList<>();
        try {
            Model model = new Model();
            model.setBrandId_("");
            model.setTypeId_("");
            model.setId_("");
            model.setName("请选择型号");
            modelList.add(model);
            List<Model> models = DaoManager.getDaoSession(MyApplication.APP_CONTEXT)
                    .getModelDao().queryBuilder().where(
                            ModelDao.Properties.BrandId_.eq(brandId),
                            ModelDao.Properties.TypeId_.eq(typeId)).list();
            if (models != null) {
                modelList.addAll(models);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return modelList;
    }
}
